package com.nulldev.util.web.YouTubeExtractor.baltimore;

import java.util.Locale;

import com.nulldev.util.VariableAPI.MathUtil.TimeUtil;
import com.nulldev.util.data.CachedItem;
import com.nulldev.util.data.Variables;
import com.nulldev.util.web.YouTubeExtractor.baltimore.BaltimoreDevices.BaltimoreDevice;

public class BaltimoreUserAgent {

	public static final BaltimoreUserAgent BALTIMORE_YOUTUBE_APP = new BaltimoreUserAgent(BaltimoreType.YOUTUBE_APP);
	public static final BaltimoreUserAgent BALTIMORE_YOUTUBE_MUSIC_APP = new BaltimoreUserAgent(BaltimoreType.YOUTUBE_MUSIC_APP);
	public static final BaltimoreUserAgent BALTIMORE_YOUTUBE_CREATORS_APP = new BaltimoreUserAgent(BaltimoreType.YOUTUBE_CREATORS_APP);

	public static final int BALTIMORE_DYNAMITE_VERSION = 59;

	/* */

	private static final CachedItem<BaltimoreDevice> CACHED_DEVICE = new CachedItem<BaltimoreDevice>("YTE-BALTIMORE", TimeUtil.fromHours(1)) {

		@Override
		public BaltimoreDevice accquire() throws Exception {
			return BaltimoreDevices.random();
		}
	};

	/* */

	private final Context a;

	public BaltimoreUserAgent(final BaltimoreType type) {
		this.a = new Context(type);
	}

	private static class Context {
		private final BaltimoreType type;

		public Context(final BaltimoreType type) {
			this.type = Variables.requireNonNullObject(type, "type");
		}

		public String getPackageName() {
			switch (this.type) {
				case YOUTUBE_APP:
					return "com.google.android.youtube";
				case YOUTUBE_MUSIC_APP:
					return "com.google.android.apps.youtube.music";
				case YOUTUBE_CREATORS_APP:
					return "com.google.android.apps.youtube.creator";
				default:
					throw new UnsupportedOperationException("unknown type: " + this.type);
			}
		}

		public String getVersionString() {
			switch (this.type) {
				case YOUTUBE_APP:
					return "19.12.41";
				case YOUTUBE_MUSIC_APP:
					return "6.45.54";
				case YOUTUBE_CREATORS_APP:
					return "24.11.103";
				default:
					throw new UnsupportedOperationException("unknown type: " + this.type);
			}
		}

		public int getVersionCode() {
			switch (this.type) {
				case YOUTUBE_APP:
					return 1545211328;
				case YOUTUBE_MUSIC_APP:
					return 64554230;
				case YOUTUBE_CREATORS_APP:
					return 124111037;
				default:
					throw new UnsupportedOperationException("unknown type: " + this.type);
			}
		}

	}

	public final String getDefaultUserAgent() {
		final BaltimoreDevice DEVICE = CACHED_DEVICE.waitFor();

		if (DEVICE == null) {
			CACHED_DEVICE.reaccquire();
			return getDefaultUserAgent();
		}

		{
			StringBuilder sb = new StringBuilder();
			Context context = this.a;
			int i = a.getVersionCode();
			sb.append(context.getPackageName());
			sb.append('/');
			sb.append(i);
			sb.append(" (Linux; U; Android ");
			sb.append(DEVICE.getRelease());
			sb.append("; ");
			sb.append(Locale.getDefault().toString());
			String str = DEVICE.getModel();
			if (str.length() > 0) {
				sb.append("; ");
				sb.append(str);
			}
			String str2 = DEVICE.getBuildId();
			if (str2.length() > 0) {
				sb.append("; Build/");
				sb.append(str2);
			}
			sb.append("; Cronet/99.0.4512.7)");
			return sb.toString();
		}
	}

	public String generate() {
		return getDefaultUserAgent();
	}

	public String getPackageName() {
		return this.a.getPackageName();
	}

	public String getPackageVersion() {
		return this.a.getVersionString();
	}

	public int getPackageVersionCode() {
		return this.a.getVersionCode();
	}

	public static BaltimoreUserAgent fromId(final String type) {
		switch (Variables.requireNonNullObject(type, "type")) {
			case "BALTIMORE_YOUTUBE":
				return BALTIMORE_YOUTUBE_APP;
			case "BALTIMORE_YOUTUBE_MUSIC":
				return BALTIMORE_YOUTUBE_MUSIC_APP;
			case "BALTIMORE_YOUTUBE_CREATORS":
				return BALTIMORE_YOUTUBE_CREATORS_APP;
			default:
				throw new UnsupportedOperationException("unknown type: " + type);
		}
	}
}
